package cn.kgc.springmvc.day03.handler;

import cn.kgc.springmvc.day03.entity.ResponseResult;
import cn.kgc.springmvc.day03.exceptions.ParamException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2024/5/7
 * @description   springmvc中全局异常处理方案
 * @ResponseBody 注解添加在类上 后续返回的结果全部是json格式
 *
 * @RestControllerAdvice  =   @ControllerAdvice +  @ResponseBody
 */
// @ControllerAdvice
// @ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler({ParamException.class})
    public ResponseResult paramExceptionHandler(ParamException e){
        String info = e.getInfo();
        return  new ResponseResult().setCode(500).setMessage(info);
    }



    @ExceptionHandler({ArithmeticException.class})
    public ResponseResult  paramExceptionHandler(ArithmeticException e){
        String message = e.getMessage();
        return  new ResponseResult().setCode(500).setMessage(message);
    }

}
